*********** Qualtrics Survey DEEP coding ***********

* make sure working directory is set to folder within which this script is saved

* Convert DEEP estimates file to Stata *

import delimited "Final DEEP HB estimates.csv"
gen id = serial_hashed
save "Final DEEP HB estimates.dta", replace

* Import survey data *

import delimited "Loss_Uncertainty_and_Political_Ideology.csv", clear
gen id = vid

* Merge with DEEP data *

drop if id==""
sort id

merge 1:1 id using "Final DEEP HB estimates.dta"
keep if _merge==3
drop _merge

*** Demographics and basic politics ***

gen straightline=0
replace straightline=1 if id=="14fa47e3-45d7-4ade-b748-ba834059d425"
replace straightline=1 if id=="b3cfaee3-1707-4cbd-945f-18f842d1b54c"
replace straightline=1 if id=="3f2bc0b9-0446-4914-8b7a-3aad107b5121"

gen age=d1
replace age="69" if age=="69 huh huh huh"
destring age, replace
recode age .49=.
recode age 2=.
recode age 3=.
recode age 4=.
replace age=. if age<18
recode01 age

gen female=d2
recode female 2=1 1=0

gen white=d3
recode white 1=1 else=0

gen black=d3
recode black 2=1 else=0

gen hisp=d3
recode hisp 3=1 else=0

gen educ=d4
recode01 educ

gen unemp=d5
recode unemp 2=1 3=1 else=0

gen union=d6
recode union 2=0

gen income=d7
recode01 income

gen interest=d8
recode01 interest

gen tpid=d10
recode tpid 1=-1 2=1 3/5=0

gen pid=.
replace pid=0 if d10==1 & d11a==1
replace pid=1 if d10==1 & d11a==2
replace pid=2 if d10>=3 & d10<=5 & d12==2
replace pid=3 if d10>=3 & d10<=5 & d12==3
replace pid=4 if d10>=3 & d10<=5 & d12==1
replace pid=5 if d10==2 & d11b==2
replace pid=6 if d10==2 & d11b==1
recode01 pid

gen ideo=d13
recode01 ideo

gen right01=(pid01+ideo01)/2

gen know1=d14
recode know1 2=1 else=0

gen know2=d15
recode know2 4=1 else=0

gen know3=d16
recode know3 1=1 else=0

gen know4=d17
recode know4 1=1 else=0

gen know5=d18
recode know5 1=1 else=0

gen know6=d19
recode know6 5=1 else=0

gen know7=d20
recode know7 3=1 else=0

egen soph=rowtotal(know1-know7)
recode01 soph

gen engage01=(soph01+interest01)/2

gen personal1=d21a
gen personal2=d21b
gen personal=(personal1+personal2)/2
recode01 personal

*** Issue attitudes ***

gen gay=i1
recode gay 1=-1 2=1 3=0
recode01 gay

gen abort=i2
recode abort 4=0 3=1 2=2 1=3
recode01 abort

gen imm=i3
recode01 imm

gen affirm=i4
recode01 affirm

gen milit=i5
recode milit 6=0 5=1 4=2 3=3 2=4 1=5
recode01 milit

gen wage=i6
recode01 wage

gen insure=i7
recode01 insure

gen imports=i8
recode01 imports

gen ssec=i9
recode ssec 6=0 5=1 4=2 3=3 2=4 1=5
recode01 ssec

gen tax=i10
recode01 tax

*** Political values ***

* Moral traditionalism *

gen trad1=p11

gen trad2=p12
replace trad2  = 8 - trad2

gen trad3=p13
replace trad3  = 8 - trad3

gen trad4=p14

* Limited government *

gen lim1=p21

gen lim2=p22
recode lim2 2=1 1=2

gen lim3=p23
recode lim3 2=1 1=2

*** Personality items ***

* Big Five (openness reverse-coded) *

gen conven=f10
replace conven = 8 - conven

gen complex=f9

gen open=complex+conven
recode01 open

* Schwartz *

gen creative=r1
gen secure=r2
gen behave=r3
gen risks=r4
gen trad=r5

recode creative 8=6
recode secure 8=6
recode behave 8=6
recode risks 8=6
recode trad 8=6

gen rr_creative = 7 - creative
gen rr_risks = 7 - risks

egen conserve=rowmean(rr_creative rr_risks secure behave trad)
replace conserve = 7 - conserve
recode01 conserve

* Authoritarianism *

gen auth1=a1
recode auth1 1=0 2=1

gen auth2=a2
recode auth2 1=0 2=1

gen auth3=a3
recode auth3 2=0

gen auth4=a4
recode auth4 1=0 2=1

gen auth5=a5
recode auth5 2=0

gen auth=(auth1+auth2+auth3+auth4+auth5)
recode01 auth

* Need for closure *

gen rr_n1 = 7 - n1
gen rr_n5 = 7 - n5
egen nclose=rowmean(rr_n1 n2-n4 rr_n5 n6-n10)
replace nclose = 7 - nclose
recode01 nclose

*** Save ***

export delimited "DEEP Data_for R.csv", replace






